package com.hisan.freeride.home.activity;

import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.databinding.ReserveBinding;
import com.hisan.freeride.home.fragment.InsuranceFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/6
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：我的保险页面
 */
public class InsuranceActivity extends BaseActivity<ReserveBinding> {
    private List<InsuranceFragment> fragments;
    private List<String> mTitles = new ArrayList<String>() {
        {
            add("未支付");
            add("已支付");
            add("已生效");
            add("已取消");
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.reserve;
    }
    @Override
    protected void initView() {
        fragments = new ArrayList<>(4);
        InsuranceFragment unpaid = InsuranceFragment.
                newInstance(0);
        InsuranceFragment Paid = InsuranceFragment.
                newInstance(1);
        InsuranceFragment inforce = InsuranceFragment.
                newInstance(2);
        InsuranceFragment cancel = InsuranceFragment.
                newInstance(-1);
        initSelect();
        fragments.add(unpaid);
        fragments.add(Paid);
        fragments.add(inforce);
        fragments.add(cancel);
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
        mBinding.viewPager.setOffscreenPageLimit(fragments.size());
        mBinding.viewPager.setCurrentItem(0);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.tab.setupWithViewPager(mBinding.viewPager);
    }

    /**
     * 初始化水平选择栏
     */
    private void initSelect() {
        //设置TabLayout的模式
        mBinding.tab.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        //需要自定义布局时，需要在绑定viewpager前加入布局
        for (int i = 0; i < mTitles.size(); i++) {
            mBinding.tab.addTab(
                    mBinding.tab.newTab().setText(mTitles.get(i)));
        }
        setIndicator(mBinding.tab,20,20);
    }

    public void setIndicator (TabLayout tabs,int leftDip,int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private List<InsuranceFragment> fragments;

        public MyPagerAdapter(FragmentManager fm, List<InsuranceFragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
    }

    @Override
    protected void loadData(boolean isRefresh) {}

}
